/********************************************
******           INSTRUCTIONS:         ******
******     Left click to place a       ******
******     particle. Right click to    ******
******     restart game.               ******
******                                 ******
********************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

class Particle {
	private final double mass;
	private double x, y;
	private double xVelocity, yVelocity, xForce, yForce;

	public Particle(double xpos, double ypos, double xvel, double yvel) {
		mass = Math.random() * 60 + 220 ;
		x = xpos;
		y = ypos;
		xVelocity = xvel;
		yVelocity = yvel;
	}

	public double getX() {
		return x;
	}
	public double getY() {
		return y;
	}
	public double getXVel() {
		return xVelocity;
	}
	public double getYVel() {
		return yVelocity;
	}
	public double getXForce() {
		return xForce;
	}
	public double getYForce() {
		return yForce;
	}
	public void setXForce(double xforce) {
		xForce = xforce;
	}
	public void setYForce(double yforce) {
		yForce = yforce;
	}
	public void update(double time) {
		xVelocity = xVelocity + (xForce / mass) * time;
		yVelocity = yVelocity + (yForce / mass) * time;
		x = x + xVelocity * time;
		y = y + yVelocity * time;

		if (y < 0) {
		 y *= -1;
		 yVelocity *= -.8;
		 yForce *= -.8;
	 	}
	 	if (x < 0) {
			x*= -1;
			xVelocity *= -.8;
			xForce *= -.8;
		}
		if ( x > 783) {
			x = 783 - x % 783 ;
		 	xVelocity *= -.8;
		 	xForce *= -.8;
		}
		if ( y > 561) {
					y = 561 - y % 561 ;
				 	yVelocity *= -.8;
				 	yForce *= -.8;
		}

	if (Math.abs(xVelocity) > 15)
		xVelocity = xVelocity > 0 ? 15 : -15;
	if (Math.abs(yVelocity) > 15)
		yVelocity = yVelocity > 0 ? 15 : -15;
	}




}

public class gravity extends JPanel implements ActionListener, MouseListener {
	ArrayList<Particle> particles;
	int fart = 0;
	int section = 0;
	int score = 0;
	int highscore = 0;
	Image img;
	Timer t;
	boolean done;
	public gravity() {
		super();
		done = false;
		particles = new ArrayList<Particle>();
		//particles.add(new Particle(0, 0, 0, 0));
		t = new Timer(10, this);

		t.start();
		setBackground(Color.black);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.green);
		if (done) {
			g.drawString("You lose", 50, 100);
			if (score > highscore)
				highscore = score;
			t.stop();
		}
		g.drawString("hi score: " + highscore, 50, 20);
		g.drawString("score: " + score, 50, 50);
		if (getMousePosition() == null) return;
		if (fart % 255 == 0) section++;


		if (section % 3 == 0) {
			g.setColor(new Color(fart % 255, 0, 255 - fart % 255));
		} else if (section % 3 == 1) {
			g.setColor(new Color(255 - fart % 255, fart % 255, 0));
		} else if (section % 3 == 2) {
			g.setColor(new Color(0, 255 - fart % 255, fart % 255));
		}


		for (Particle each: particles) {
			each.setXForce(150 * Math.cos(Math.atan2(getMousePosition().getY()-each.getY(), getMousePosition().getX()-each.getX())));
			each.setYForce(150 * Math.sin(Math.atan2(getMousePosition().getY()-each.getY(), getMousePosition().getX()-each.getX())));
			each.update(.5);
			g.fillRect((int)each.getX(), (int)each.getY(), 20, 20);
			if ((int)each.getX() >= (int)getMousePosition().getX() && (int)each.getX() <= (int)getMousePosition().getX() + 20
				&& (int)each.getY() >= (int)getMousePosition().getY() && (int)each.getY() <= (int)getMousePosition().getY() + 20)
				done = true;
		}

	}
	public void actionPerformed(ActionEvent e) {
		fart +=5;

		if (fart % 235 == 0){
			if (score % 50 == 0) particles.add(new Particle(0, 0, 0, 0));
			score+=5;
		}

		repaint();
	}
	public void mouseExited(MouseEvent e) { if (t.isRunning()) t.stop(); }
	public void mouseEntered(MouseEvent e) { if (!done) t.start(); }
	public void mouseReleased(MouseEvent e) { }
	public void mouseClicked(MouseEvent e) { }

	public void mousePressed(MouseEvent e) {
		if (done && e.getButton() == MouseEvent.BUTTON3) {
			score = 0;
			particles = new ArrayList<Particle>();
			done = false;
			t.start();
			return;
		}
		score+=20;
		particles.add(new Particle(e.getX(), e.getY() - 20, 0, -50));
	}
	public static void main(String[] args) {
			JFrame w = new JFrame("Particle");
			w.setSize(800,600);
			w.setLocationRelativeTo(null);
			w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			gravity lol = new gravity();
			w.add(lol);
			lol.requestFocus();
			w.addMouseListener(lol);
			w.setVisible(true);
		}
}